/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： LocalStaticResourceService.java
 * Date：18-11-5 上午10:54
 * Author: guomw
 */

package com.guomw.report.service.service.upload.impl;

import com.guomw.report.common.utils.PropertyUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.context.WebApplicationContext;


import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @author guomw
 * @date 2018/9/26
 */
@Service
@Profile("!container")
public class LocalStaticResourceService extends AbstractStaticResourceService {
    private static final Log log = LogFactory.getLog(LocalStaticResourceService.class);

    @Autowired
    public void setWebApplicationContext(WebApplicationContext context) {
        File file = new File(context.getServletContext().getRealPath("/resource"));
        this.fileHome = file.toURI();
        String uri = PropertyUtil.getProperty("localResourcesUri", "http://localhost:8080/resource");
        if (uri == null || StringUtils.isEmpty(uri)) {
            throw new IllegalStateException("resourcesUri属性");
        }
        try {
            this.uriPrefix = new URI(uri);
        } catch (URISyntaxException e) {
            log.error("解析失败", e);
            throw new InternalError("解析失败");
        }
    }
}
